//package com.dfbz.filter;
//
//import com.dfbz.dao.baseDao;
//import com.dfbz.utils.DataSourceUtils;
//import org.apache.http.HttpRequest;
//import org.apache.http.HttpResponse;
//import org.springframework.jdbc.core.RowMapper;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.lang.reflect.InvocationHandler;
//import java.lang.reflect.Method;
//import java.lang.reflect.Proxy;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.ArrayList;
//import java.util.List;
//
//@WebFilter("/*")
//public class TextFilter extends baseDao implements Filter {
//    //存储脏话的集合
//    private List<String> words = new ArrayList<>();
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//
//        try {
//            Connection connection = DataSourceUtils.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement("select * from words");
//            ResultSet resultSet = preparedStatement.executeQuery();
//            while (resultSet.next()) {
//                words.add(resultSet.getString("word"));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request=(HttpServletRequest)servletRequest;
//        HttpServletResponse response=(HttpServletResponse) servletResponse;
//        servletRequest.setCharacterEncoding("UTF-8");
//        ServletRequest o = (ServletRequest) Proxy.newProxyInstance(
//                //和目标一样的加载器
//                TextFilter.class.getClassLoader(),
//                servletRequest.getClass().getInterfaces(),
//                new InvocationHandler() {
//                    @Override
//                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                        String name = method.getName();
//                        if ("getParameter".equals(name)) {
//                            String invoke = (String) method.invoke(servletRequest, args);
//                            //增强方法
//                            for (String word : words) {
//                                if(invoke.contains(word)){
//                                    name = invoke.replaceAll(word, "***");
//                                }
//                            }
//                            return name;
//                        }
//                        return method.invoke(servletRequest,args);
//                    }
//                }
//        );
//        filterChain.doFilter(o, servletResponse);
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//}
